#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
pii m[26];
int main() {
    //ios_base::sync_with_stdio(false);cin.tie(nullptr);
    int N; cin >> N;
    for (int i=0; i<N; ++i){
        char ch;cin >> ch;
        cin >> m[ch-'A'].first >> m[ch-'A'].second;
    }
    string cmd;
    while(cin >> cmd){
        stack<pii> s;
        long long ans = 0;
        bool error = false;
        for (int i = 0; i < cmd.size(); ++i) {
            if(cmd[i] == ')'){
                pii a, b;
                b = s.top(); s.pop();
                a = s.top(); s.pop();
                if(a.second == b.first){
                    ans += (long long)a.first * b.first * b.second;
                    s.push(make_pair(a.first, b.second));
                }
                else{
                    error = true;
                    cout << "error\n";
                    break;
                }
            }
            else if(cmd[i] != '('){
                int id = cmd[i] - 'A';
                s.push(make_pair(m[id].first, m[id].second));
            }
        }
        if(!error) cout << ans << "\n";
    }
    return 0;
}